package cn.com.server.config;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.util.ResourceUtils;  
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;  
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import cn.com.server.config.bean.AppResources;  
  
/**
 * 配置静态资源
 * @author admin
 */
@Configuration  
public class WebConfig extends WebMvcConfigurerAdapter {
	
	@Autowired
	private AppResources appResources;
  
    @Override  
    public void addResourceHandlers(ResourceHandlerRegistry registry) {  
    	if(appResources.getResourceHandlerMap()!=null&&appResources.getResourceHandlerMap().size()>0) {
    		for(Map<String, String> m:appResources.getResourceHandlerMap()) {
    			registry.addResourceHandler(m.get("staticName")).addResourceLocations(m.get("staticPath"));
    		}
    	}else {
    		registry.addResourceHandler("/templates/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/templates/");  
    		registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX+"/static/");  
    	}
        super.addResourceHandlers(registry);  
    }  
    
    @Bean(name = "mappingJacksonHttpMessageConverter")
   	public MappingJackson2HttpMessageConverter myMappingJacksonHttpMessageConverter(){
       	MappingJackson2HttpMessageConverter mjhmc = new MappingJackson2HttpMessageConverter();
       	List<MediaType> supportedMediaTypes = new ArrayList<>();
       	MediaType m = new MediaType("text%2fhtml%3bcharset%3dUTF-8");
       	MediaType m2 = new MediaType("application%2fjson%3bcharset%3dUTF-8");
       	supportedMediaTypes.add(m);
       	supportedMediaTypes.add(m2);
       	mjhmc.setSupportedMediaTypes(supportedMediaTypes);
       	return mjhmc;
       }
  
} 
